package io.zw.modules.xinpai.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.zw.common.utils.R;
import io.zw.modules.sys.controller.AbstractController;
import io.zw.modules.xinpai.entity.CarDataEntity;
import io.zw.modules.xinpai.entity.CarEntity;
import io.zw.modules.xinpai.entity.UserDataEntity;
import io.zw.modules.xinpai.entity.UsersEntity;
import io.zw.modules.xinpai.service.CarDataService;
import io.zw.modules.xinpai.service.CarService;
import io.zw.modules.xinpai.service.UserDataService;
import io.zw.modules.xinpai.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;

/**
 * 创建人:zhuwei
 * 邮箱:121203654@qq.com
 * 创建时间:2018-10-22 15:52
 */
@RestController
@RequestMapping("/api")
public class TcpApiController extends AbstractController {

    @Autowired
    private CarService carService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private CarDataService carDataService;
    @Autowired
    private UserDataService userDataService;
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;
    /**
     * 车辆位置更新
     */
    @PostMapping("cargps")
    public void CarGpsUpdate(String imei,String lng,String data){
        CarEntity car=new CarEntity();
        car.setImei(imei);
        car.setLocation(lng);
        CarDataEntity carDataEntity= JSON.parseObject(data,CarDataEntity.class);
        carDataEntity.setDataTime(new Date());
        carService.updateById(car);
        CarEntity finalCar=carService.selectById(imei);
        car.setState(1);
        R r=new R();
        r.put("data",carDataEntity);
        r.put("car",car);
        simpMessagingTemplate.convertAndSendToUser(finalCar.getFirmId()+"","/gps",r);
        carDataEntity.setImei(imei);
        carDataService.insert(carDataEntity);
    }
    /**
     * 工牌位置更新
     */
    @PostMapping("usergps")
    public void usersGpsUpdate(String imei,String lng,String data){
        UsersEntity user=new UsersEntity();
        user.setLocation(lng);
        UserDataEntity userDataEntity=JSON.parseObject(data,UserDataEntity.class);
        userDataEntity.setDataTime(new Date());
        usersService.update(user,new EntityWrapper<UsersEntity>().eq("imei",imei));
        userDataEntity.setUserId(imei);
        userDataService.insert(userDataEntity);
        user.setState(1);
        user.setImei(imei);
        UsersEntity usersE=usersService.selectOne(new EntityWrapper<UsersEntity>().eq("imei",imei));
        simpMessagingTemplate.convertAndSendToUser(usersE.getFirmId()+"","/shouhuan",new R().put("user",user));
    }
    /**
     * 在线状态更新
     */
    @PostMapping("getstate")
    public void updateState(String tick,String imei,Integer state){
        if(tick.indexOf("car")!=-1){
            CarEntity car=new CarEntity();
            car.setState(state);
            car.setImei(imei);
            carService.updateById(car);
        }else if(tick.indexOf("user")!=-1){
            UsersEntity usersEntity=new UsersEntity();
            usersEntity.setState(state);
            usersService.update(usersEntity,new EntityWrapper<UsersEntity>().eq("imei",imei));
            usersEntity.setImei(imei);
            UsersEntity user=usersService.selectOne(new EntityWrapper<UsersEntity>().eq("imei",imei));
            simpMessagingTemplate.convertAndSendToUser(user.getFirmId()+"","/shouhuan",new R().put("user",usersEntity));
        }

    }
}
